package com.yc.homepage.service.impl;

import com.yc.homepage.CourseInfo;
import com.yc.homepage.CourseInfosRequest;
import com.yc.homepage.UserInfo;
import com.yc.homepage.client.CourseClient;
import com.yc.homepage.dao.HomepageUserCourseDao;
import com.yc.homepage.dao.HomepageUserDao;
import com.yc.homepage.entity.HomepageUser;
import com.yc.homepage.entity.HomepageUserCourse;
import com.yc.homepage.service.IUserService;
import com.yc.homepage.vo.CreateUserRequest;
import com.yc.homepage.vo.UserCourseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <h1>用户相关服务实现</h1>
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    /** HomepageUser Dao */
    private final HomepageUserDao homepageUserDao;

    /** HomepageUserCourse Dao */
    private final HomepageUserCourseDao homepageUserCourseDao;

    /** CourseClient courseClient */
    private final CourseClient courseClient;

    @Autowired
    public UserServiceImpl(HomepageUserDao homepageUserDao, HomepageUserCourseDao homepageUserCourseDao,
                           CourseClient courseClient) {
        this.homepageUserDao = homepageUserDao;
        this.homepageUserCourseDao = homepageUserCourseDao;
        this.courseClient = courseClient;
    }

    /**
     * 创建用户信息
     * @param request {@link CreateUserRequest}
     * @return
     */
    @Override
    public UserInfo createUser(CreateUserRequest request) {

        if (!request.validate()) {
            return UserInfo.invalid();
        }

        HomepageUser oldUser = homepageUserDao.findByUsername(request.getUsername());
        if (null != oldUser) {
            return UserInfo.invalid();
        }

        HomepageUser newUser = homepageUserDao.save(new HomepageUser(
                request.getUsername(), request.getEmail()
        ));

        return new UserInfo(newUser.getId(), newUser.getUsername(), newUser.getEmail());
    }

    @Override
        public UserInfo getUserInfo(Long id) {

            Optional<HomepageUser> user = homepageUserDao.findById(id);
            if (!user.isPresent()) {
            return UserInfo.invalid();
        }

        HomepageUser curUser = user.get();
        return new UserInfo(curUser.getId(), curUser.getUsername(), curUser.getEmail());
    }

    @Override
    public UserCourseInfo getUserCourseInfo(Long id) {

        Optional<HomepageUser> user = homepageUserDao.findById(id);
        // 判断用户是否存在
        if (!user.isPresent()) {
            return UserCourseInfo.invalid();
        }

        // 构建一个UserInfo对象
        HomepageUser homepageUser = user.get();
        UserInfo userInfo = new UserInfo(
                homepageUser.getId(),
                homepageUser.getUsername(),
                homepageUser.getEmail());

        // 通过用户课程dao(userCourseDao) 获取用户课程信息
        List<HomepageUserCourse> userCourses = homepageUserCourseDao.findAllByUserId(id);
        if (CollectionUtils.isEmpty(userCourses)) {
            return new UserCourseInfo(userInfo, Collections.emptyList());
        }

        // 通过查询的课程po获取课程信息
        List<CourseInfo> courseInfos = courseClient.getCourseInfos(
                new CourseInfosRequest(
                        userCourses.stream()
                                .map(HomepageUserCourse::getCourseId)
                                .collect(Collectors.toList())
                )
        );

        // 封装用户课程vo
        return new UserCourseInfo(userInfo, courseInfos);
    }
}
